<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\Weibo as WeiboModel;
use app\common\model\Member as MemberModel;
use think\Db;
use think\Controller;

/**
 * 微博登录控制器
 * Class Weibo
 * @package app\home\controller
 */
class Weibo extends Controller
{
    /**
     * [login  微博登录]
     * @return json
     * @author [C。]
     */
    public function login()
    {
        $param = input('param.');
        $model = new WeiboModel();
        $mmodel = new MemberModel();
        try{
            $qq = $model->findOne(['openid'=>$param['openid']]);
            if(empty($qq)){
                $param['nickname'] = filter_Emoji($param['nickname']);
                $id = $model->addData($param);
            }else{
                if(empty($qq['mid'])){
                    $id = $qq['id'];
                }else{
                    $member = $mmodel->findData($qq['mid'],'id','token');
                    return admin_json(1, '登录成功',$member['token']);
                }
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/Weibo/login');
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        return admin_json(2, '请绑定手机',['id'=>$id,'type'=>3]);
    }
    /**
     * [verifytel  验证手机]
     * @return json
     * @author [C。]
     */
    public function bindtel()
    {
        $param = input('param.');
        $model = new WeiboModel();
        $mmodel = new MemberModel();
        $map = [];
        $map1 = [];
        try{
            $map['tel'] = $param['tel'];
            $map['code'] = $param['code'];

            $map1['tel'] = $param['tel'];

            $mem = Db::name('member')->where($map1)->find();
            if(!empty($mem)){
                return admin_json('-4','手机号已注册！');
            }
            $result = Db::name('vercode')->where($map)->find();
            if(!empty($result)) {
                if ($result['status'] == 2) {
                    return admin_json('-3', '验证码已使用');
                }
                if (time() - $result['addtime'] > config('home.code_expiration_time')) {
                    return admin_json('-2', '验证码已过期');
                }
                Db::name('vercode')->where($map)->update(['status' => 2]);
            }else{
                return admin_json('-1','手机号或验证码错误');
            }
            $qq = $model->findData($param['id'],'id');
            $darr = array();
            $nowtime = time();
            $darr['pswd'] = md5($param['pswd']);
            $darr['token'] = md5('app' . $param['tel'] . config('home.token_halt'));
            $darr['tokentime'] = $nowtime;
            $darr['eqno'] = $param['eqno'];
            $darr['eqtype'] = $param['eqtype'];
            $darr['username'] = $qq['nickname'];
            $darr['tel'] = $param['tel'];
            $darr['addtime'] = $nowtime;
            $filename = saveImageFromHttp($qq['headimgurl']);
            $darr['img'] = uploadoss($filename);
            $id = $mmodel->addData($darr);
            $model->editData(['id'=>$qq['id'],'mid'=>$id]);
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/Weibo/bindtel');
            return admin_json(-1, '绑定失败'.$e->getMessage());
        }
        return admin_json(1, '绑定成功',$darr['token']);
    }
    /**
     * [bindweibo  绑定微博]
     * @return json
     * @author [C。]
     */
    public function bindweibo()
    {
        $param = input('param.');
        $model = new WeiboModel();
        $mmodel = new MemberModel();
        try{
            $member = $mmodel->findOne(['token'=>$param['token']],'id');
            $weibo1 = $model->findOne(['mid'=>$member['id']]);
            if(!empty($weibo1)){
                return admin_json(-1, '已绑定微信');
            }
            $weibo = $model->findOne(['openid'=>$param['openid']]);
            if(!empty($weibo['mid'])){
                return admin_json(-1, '该微博已绑定其他用户！');
            }
            if(empty($weibo)){
                $param['mid'] = $member['id'];
                $param['nickname'] = filter_Emoji($param['nickname']);
                $model->addData($param);
            }else{
                if(empty($weibo['mid'])){
                    $param['mid'] = $member['id'];
                    $param['id'] = $weibo['id'];
                    if(!empty($param['nickname'])){
                        $param['nickname'] = filter_Emoji($param['nickname']);
                    }
                    $model->editData($param);
                }

            }
        }catch(\Exception $e){
            systembug($e->getMessage(),2,'home/Weibo/bindweibo');
            return admin_json(-1, '绑定失败'.$e->getMessage());
        }
        return admin_json(1, '绑定成功');
    }
}